﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class 数据算法_直接插入排序 : System.Web.UI.Page
{
    /// <summary>
    /// 初始化int数组
    /// </summary>
    int[] SordData = { 57, 0, 56, 32423, 21, 990, 3434, 256, 466, 798 };

    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Write("MaoPao:" );

      Sort_MoPao(SordData);

        Sort(" 57, 0, 56, 32423, 21, 990, 3434, 256, 466, 798 ");
        
    }
    ///开始冒泡
    public void Sort_MoPao(int[] shu)
    {
        int temp =0;
        for (int i = 0; i < shu.Length; i++)
        {
            for (int j = 0; j < shu.Length - 1; j++)
            {
                if (shu[j + 1] < shu[j])//如果后面的小
                {
                    temp = shu[j];
                    shu[j] = shu[j + 1];
                    shu[j + 1] = temp;
                }
            }

        }
        foreach (int ss in shu)
        {
            Response.Write(ss + ",");
        }
    }

    /// <summary>
    /// 直接选择排序
    /// </summary>
    /// <param name="strx">傳入的字符串參數，用，把數字格開，如 1,2,3</param>
    public void Sort(string strx)
    {

        int i, j,  交換, 循环;
        string[] shu = strx.Split(',');
        
        ///如果要把序排好的话一共要进行的最多排序次数
        for ( i = 1; i < shu.Length; i++)
        {
            循环 = i;
            交換 = int.Parse(shu[i]);
            ///如果 循环 大于 0 （不是第一次循环）  而且  当前的数比以前的数据小的话
            ///就一直把这个当前的数向后放，放到全适的位置
          
            while (循环 > 0 && int.Parse(shu[循环 - 1]) >= 交換)
            { 
                shu[循环] = shu[循环 - 1];  

                循环--;
            }

           shu[循环] = 交換.ToString();

                
          }

        foreach (string ss in shu)
        {
            Response.Write(ss + ",");
        }

      
        
        


    }
}
